﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FrameWork.Plugin;
using FrameWork.Setting;
using FrameWork.Log;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ComponentModel;
using FrameWork.Output;
using System.Windows.Forms;
using ScreenMonitor.ServerLib.Plugin;
using ScreenMonitor.ServerLib;
using System.Net.Sockets;

namespace ProcessManagerPlugin.Server
{
    [SettingParentDefine]
    public partial class ProcessManagerPlugin : LogableClassBase, IServerPlugin
    {
        /// <summary>
        /// 是否自动刷新客户端进程列表
        /// </summary>
        [SettingItemDefine(SettingValueType.Bool, true)]
        [DisplayName("自动刷新列表")]
        [Description("获取或设置是否在客户端列表选择客户端之后自动刷新该客户端的进程列表")]
        [DefaultValue(true)]
        [Browsable(true)]
        public bool AutoRefreshList { get; set; }

        public string GetPluginName()
        {
            return "进程管理服务端插件";
        }

        public Guid GetPluginGuid()
        {
            return new Guid("5A209AAA-F14C-4973-A9B1-DD13D3F6F05F");
        }

        public string GetPluginDescription()
        {
            return "进程管理服务端插件";
        }

        public List<FrameWork.Setting.SettingEditorItem> GetSettingEditors()
        {
            List<SettingEditorItem> EditorList = new List<SettingEditorItem>();
            EditorList.Add(new SettingPropertyEditorItem(@"进程管理\常规", this, this.GetType()));
            return EditorList;
        }

        public System.Windows.Forms.Form GetPluginWindow()
        {
            return PluginObjects.PluginWnd;
        }

        public IEnumerable<FrameWork.Output.OutputCollectorBase> GetOutputCollectors()
        {
            List<OutputCollectorBase> List = new List<OutputCollectorBase>();
            List.Add(PluginObjects.ProcessManagerPluginOutputCollector);
            return List;
        }

        public bool Enable()
        {
            base.PrintLog(LogType.Info, "进程管理插件已启用！");
            return true;
        }

        public bool Disable()
        {
            base.PrintLog(LogType.Info, "进程管理插件已禁用！");
            return true;
        }

        public bool Init()
        {
            base.PrintLog(LogType.Info, "进程管理插件初始化...");
            ServerUI.Program.ClientList.clientListTView.AfterSelect += clientListTView_AfterSelect;
            return true;
        }
        public void GetClientProcessList()
        {
            TreeView tv = ServerUI.Program.ClientList.clientListTView;
            if (
                tv.SelectedNode == null ||
                tv.SelectedNode.Tag == null ||
                tv.SelectedNode.Tag.GetType() != typeof(ClientObject))
                return;
            ClientObject clientobj = (ClientObject)tv.SelectedNode.Tag;
            TcpClient client = clientobj.Client;
            ScreenMonitor.CommandObjects.TcpCommandCenter.SendCommand(Common.CommandType.GetProcessList, client);
        }

        void clientListTView_AfterSelect(object sender, TreeViewEventArgs e)
        {   
            if(AutoRefreshList)
                GetClientProcessList();
        }


        public void SetMenu(System.Windows.Forms.MenuStrip MainMenuStrip)
        {
            ToolStripItem[] MenuItems = MainMenuStrip.Items.Find("ToolsMenu", false);
            if (MenuItems.Count() > 0)
            {
                ToolStripDropDownItem ToolsMenu = (ToolStripDropDownItem)MenuItems[0];
                ToolsMenu.DropDownItems.Add(PluginObjects.PluginWnd.toolsProManage);
            }
        }

        public List<string> GetCommands()
        {
            List<string> lst = new List<string>();
            lst.Add(Common.CommandType.PostProcessList);
            return lst;
        }

        public void RunCommand(string Command, byte[] Data)
        {
            switch (Command)
            {
                case Common.CommandType.PostProcessList:
                    OperatePostList(Data);
                    break;
            }
        }
    }
}
